using System;
using System.Collections.Generic;
using System.Text;
using WebCrawlerTools.plugin;
using WebCrawlerTools.page;
using WebCrawlerTools.control;
using WebCrawlerTools.form;
using System.Windows.Forms;

namespace ReinsPlugin
{
    public class PlugIn : IPlugin
    {
        private string m_strName;
        private IPluginHost m_Host;
        private ReinsData reinsData;
        private ControlCenter ctrlCenter;
        public PlugIn()
        {
            m_strName = "Reins Plugin";
        }

        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }

        public void Show(object sender)
        {
            MainForm mf = new MainForm();
            mf.ReinsData = reinsData;
            if (mf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (sender.GetType() == typeof(ControlCenter))
                {
                    ctrlCenter = (ControlCenter)sender;
                    PHomePage homePage =ctrlCenter.HomePage;
                    if (homePage != null)
                    {
                        HForm hf = reinsData.SearchData.getForm();
                        if (hf != null)
                        {
                            homePage.SearchPage.Forms.Clear();
                            homePage.SearchPage.Forms.Add(reinsData.SearchData.getForm());
                        }
                    }
                    ctrlCenter.Engine.AllPageCompleted += new EventHandler(this.OnListPageAllCompleted);
                }
            }
        }

        public void OnListPageAllCompleted(object sender, EventArgs e)
        {
            PHomePage homePage = ctrlCenter.HomePage;
            if (homePage != null)
            {
                HForm hf = reinsData.SearchData.getNextForm();
                if (hf != null)
                {
                    homePage.SearchPage.Forms.Clear();
                    homePage.SearchPage.Forms.Add(hf);
                    ctrlCenter.StartEngine();
                }
            }
        }
        public IPluginHost Host
        {
            get { return m_Host; }
            set
            {
                m_Host = value;
                m_Host.Register(this);
            }
        }
        public void Onload()
        {
            initialize();
        }
        public void OnClose()
        {
            ReinsData.Store(reinsData);
        }
        public void initialize()
        {
            reinsData = ReinsData.Load();
        }
    }
}
